package cn.tedu.day01;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test02 {
//    @Before
//    public void start(){
//        ApplicationContext ac=
//                new ClassPathXmlApplicationContext("applicationContext.xml");
//    }
//    @After
//    public void end(){
//        ((ClassPathXmlApplicationContext)ac).close();
//    }
    @Test
    public void test01(){
   ApplicationContext ac=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        //IOC控制反转：1.多个对象，获取一个bean：同一个对象
       Person p1=(Person)ac.getBean("person");
        System.out.println(p1);
        Person p2=(Person)ac.getBean("person1");
        System.out.println(p2);
        ((ClassPathXmlApplicationContext)ac).close();
    }


    @Test
    public void test02(){
        ApplicationContext ac=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Demo d=(Demo)ac.getBean("demo");
        System.out.println(d);
        ((ClassPathXmlApplicationContext)ac).close();
    }
    /*测试  Spring IOC 控制反转---支持工厂模式：静态工厂--使用方法*/
    @Test
    public void test03(){
        ApplicationContext ac=
                new ClassPathXmlApplicationContext("applicationContext.xml");
         //调用静态方法
        NetConnection d=(NetConnection)ac.getBean("nc");
        System.out.println(d);
        ((ClassPathXmlApplicationContext)ac).close();
    }
    /*测试  Spring IOC 控制反转---支持工厂模式：实例工厂--使用方法*/
    @Test
    public void test04(){
        ApplicationContext ac=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        //调用静态方法
        NetConnection d=(NetConnection)ac.getBean("nci");
        System.out.println(d);
        ((ClassPathXmlApplicationContext)ac).close();
    }
    /*测试  Spring IOC 控制反转---支持Spring工厂模式：Spring工厂--使用方法*/
    @Test
    public void test05(){
        ApplicationContext ac=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        //使用Spring工厂模式
        NetConnection d=(NetConnection)ac.getBean("snc");
        System.out.println(d);
        ((ClassPathXmlApplicationContext)ac).close();
    }
    /*测试  单例与多例问题*/
    @Test
    public void test06(){
        ApplicationContext ac=
                new ClassPathXmlApplicationContext("applicationContext.xml");
       Person p1=(Person)ac.getBean("person");
        System.out.println(p1);
        Person p2=(Person)ac.getBean("person");
        System.out.println(p2);


        ((ClassPathXmlApplicationContext)ac).close();
    }
}
